﻿//-----------------------------------------------------------------------
// <copyright file="Categories.cs" company="Craig Longford (deltawolf7@gmail.com)">
//     Copyright (c) Craig Longford. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Delta.Mercury.Business.Commerce
{
    using System;
    using System.Collections.Generic;
    using Delta.Data.DataExpress;

    /// <summary>
    /// Collection of categories
    /// </summary>
    [Serializable]
    public class Categories : List<ICategory>, IComparer<ICategory>
    {
        #region Constructors
        /// <summary>
        /// Initializes a new instance of the Categories class
        /// </summary>
        public Categories()
        {
        }
        #endregion

        #region Load Methods
        /// <summary>
        /// Load categories from database by parent
        /// </summary>
        /// <param name="parentCategory">Parent category</param>
        /// <param name="showDeleted">Show deleted</param>
        /// <returns>Collection of categories</returns>
        public static Categories Load(ICategory parentCategory, bool showDeleted)
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Parent", parentCategory.Guid));
            parameters.Add(new Parameter("Deleted", showDeleted));
            return DataAccess.GetCollection<Categories, Category>("LoadCatgeories", System.Data.CommandType.StoredProcedure, parameters);
        }

        /// <summary>
        /// Load categories from database by product
        /// </summary>
        /// <param name="product">Product identifier</param>
        /// <param name="showDeleted">Show deleted</param>
        /// <returns>Collection of categories</returns>
        public static Categories Load(IProduct product, bool showDeleted)
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Product", product.Guid));
            parameters.Add(new Parameter("Deleted", showDeleted));
            return DataAccess.GetCollection<Categories, Category>("LoadCatgeories", System.Data.CommandType.StoredProcedure, parameters);
        }

        /// <summary>
        /// Load categories from database (root)
        /// </summary>
        /// <param name="showDeleted">Show deleted</param>
        /// <returns>Colelction of categories</returns>
        public static Categories Load(bool showDeleted)
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Deleted", showDeleted));
            return DataAccess.GetCollection<Categories, Category>("LoadCatgeories", System.Data.CommandType.StoredProcedure, parameters);
        }
        #endregion

        #region Search Methods
        /// <summary>
        /// Search categories
        /// </summary>
        /// <param name="query">Search query</param>
        /// <param name="showDeleted">Show deleted</param>
        /// <returns>Collection of categories</returns>
        public static Categories Search(string query, bool showDeleted)
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Query", query));
            parameters.Add(new Parameter("Deleted", showDeleted));
            return DataAccess.GetCollection<Categories, Category>("SearchCategories", System.Data.CommandType.StoredProcedure, parameters);
        }

        /// <summary>
        /// Search categories
        /// </summary>
        /// <param name="parent">Parent category</param>
        /// <param name="query">Search query</param>
        /// <param name="showDeleted">Show deleted</param>
        /// <returns>Collection of categories</returns>
        public static Categories Search(ICategory parent, string query, bool showDeleted)
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Parent", parent.Guid));
            parameters.Add(new Parameter("Query", query));
            parameters.Add(new Parameter("Deleted", showDeleted));
            return DataAccess.GetCollection<Categories, Category>("SearchCategories", System.Data.CommandType.StoredProcedure, parameters);
        }
        #endregion

        #region Compare Methods
        /// <summary>
        /// Compare objects
        /// </summary>
        /// <param name="x">First object</param>
        /// <param name="y">Second object</param>
        /// <returns>Match or not</returns>
        public int Compare(ICategory x, ICategory y)
        {
            return x.Name.CompareTo(y.Name);
        }
        #endregion
    }
}
